/*#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>*/
#include "cubeprimitive.h"
//#include <iostream>

using namespace std;

CubePrimitive::CubePrimitive()
{
	size = 1;

	xRot = 0;
	yRot = 0;
	zRot = 0;

	xPos = 0;
	yPos = 0;
	zPos = 0;
}

CubePrimitive::CubePrimitive(double sideLength)
{
	size = sideLength;

	xRot = 0;
	yRot = 0;
	zRot = 0;

	xPos = 0;
	yPos = 0;
	zPos = 0;

	loadDefaultTexture();
}

CubePrimitive::CubePrimitive(double sideLength, double x, double y, double z)
{
	size = sideLength;

	xRot = 0;
	yRot = 0;
	zRot = 0;

	xPos = x;
	yPos = y;
	zPos = z;
}

CubePrimitive::CubePrimitive(double x, double y, double z)
{
	size = 1;

	xRot = 0;
	yRot = 0;
	zRot = 0;

	xPos = x;
	yPos = y;
	zPos = z;
}

CubePrimitive::~CubePrimitive()
{
	cout << "destroyed" << endl;
}

void CubePrimitive::draw()
{
	//glPushMatrix();

	glTranslated(xPos, yPos, zPos);

	glRotated(xRot, 1, 0, 0);
	glRotated(yRot, 0, 1, 0);
	glRotated(zRot, 0, 0, 1);



	//glBindTexture(GL_TEXTURE_2D, texID);

	glEnable(GL_TEXTURE_2D);

	glBegin(GL_QUADS);
	//glBegin(GL_LINE_STRIP);

		//glNormal3d(0,0,-1);
		glColor4f(r, g, b, 1.0f);
		glVertex3f(-size, -size,  size);
		glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
		glMultiTexCoord2f(GL_TEXTURE1, 0, 0);
		glTexCoord2i(0,0);

		glVertex3f( size, -size,  size);
		glMultiTexCoord2f(GL_TEXTURE0, 1, 0);
		glMultiTexCoord2f(GL_TEXTURE1, 1, 0);

		glVertex3f( size,  size,  size);
		glMultiTexCoord2f(GL_TEXTURE0, 1, 1);
		glMultiTexCoord2f(GL_TEXTURE1, 1, 1);

		glVertex3f(-size,  size,  size);
		glMultiTexCoord2f(GL_TEXTURE0, 0, 1);
		glMultiTexCoord2f(GL_TEXTURE1, 0, 1);


		// Back Face
		//glColor4f(1.0f,0.0f,0.0f,0.5f);
		glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
		glMultiTexCoord2f(GL_TEXTURE1, 0, 0);
		glVertex3f(-size, -size, -size);

		glMultiTexCoord2f(GL_TEXTURE0, 1, 0);
		glMultiTexCoord2f(GL_TEXTURE1, 1, 0);
		glVertex3f(-size,  size, -size);

		glMultiTexCoord2f(GL_TEXTURE0, 1, 1);
		glMultiTexCoord2f(GL_TEXTURE1, 1, 1);
		glVertex3f( size,  size, -size);

		glMultiTexCoord2f(GL_TEXTURE0, 0, 1);
		glMultiTexCoord2f(GL_TEXTURE1, 0, 1);
		glVertex3f( size, -size, -size);
		// Top Face
		//glColor4f(0.0f,0.0f,1.0f,0.5f);
		glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
		glMultiTexCoord2f(GL_TEXTURE1, 0, 0);
		glVertex3f(-size,  size, -size);

		glMultiTexCoord2f(GL_TEXTURE0, 1, 0);
		glMultiTexCoord2f(GL_TEXTURE1, 1, 0);
		glVertex3f(-size,  size,  size);

		glMultiTexCoord2f(GL_TEXTURE0, 1, 1);
		glMultiTexCoord2f(GL_TEXTURE1, 1, 1);
		glVertex3f( size,  size,  size);

		glMultiTexCoord2f(GL_TEXTURE0, 0, 1);
		glMultiTexCoord2f(GL_TEXTURE1, 0, 1);
		glVertex3f( size,  size, -size);

		// Bottom Face
		//glColor4f(0.0f,1.0f,1.0f,.5f);
		glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
		glMultiTexCoord2f(GL_TEXTURE1, 0, 0);
		glVertex3f(-size, -size, -size);

		glMultiTexCoord2f(GL_TEXTURE0, 1, 0);
		glMultiTexCoord2f(GL_TEXTURE1, 1, 0);
		glVertex3f( size, -size, -size);

		glMultiTexCoord2f(GL_TEXTURE0, 1, 1);
		glMultiTexCoord2f(GL_TEXTURE1, 1, 1);
		glVertex3f( size, -size,  size);

		glMultiTexCoord2f(GL_TEXTURE0, 0, 1);
		glMultiTexCoord2f(GL_TEXTURE1, 0, 1);
		glVertex3f(-size, -size,  size);
		// Right face
		//glColor4f(1.0f,1.0f,0.0f,0.5f);
		glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
		glMultiTexCoord2f(GL_TEXTURE1, 0, 0);
		glVertex3f( size, -size, -size);

		glMultiTexCoord2f(GL_TEXTURE0, 1, 0);
		glMultiTexCoord2f(GL_TEXTURE1, 1, 0);
		glVertex3f( size,  size, -size);

		glMultiTexCoord2f(GL_TEXTURE0, 1, 1);
		glMultiTexCoord2f(GL_TEXTURE1, 1, 1);
		glVertex3f( size,  size,  size);

		glMultiTexCoord2f(GL_TEXTURE0, 0, 1);
		glMultiTexCoord2f(GL_TEXTURE1, 0, 1);
		glVertex3f( size, -size,  size);
		// Left Face
		//glColor4f(1.0f,1.0f,1.0f,0.5f);
		glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
		glMultiTexCoord2f(GL_TEXTURE1, 0, 0);
		glVertex3f(-size, -size, -size);

		glMultiTexCoord2f(GL_TEXTURE0, 1, 0);
		glMultiTexCoord2f(GL_TEXTURE1, 1, 0);
		glVertex3f(-size, -size,  size);

		glMultiTexCoord2f(GL_TEXTURE0, 1, 1);
		glMultiTexCoord2f(GL_TEXTURE1, 1, 1);
		glVertex3f(-size,  size,  size);

		glMultiTexCoord2f(GL_TEXTURE0, 0, 1);
		glMultiTexCoord2f(GL_TEXTURE1, 0, 1);
		glVertex3f(-size,  size, -size);

	glEnd();

	glDisable(GL_TEXTURE_2D);

	//glPopMatrix();
}

void CubePrimitive::setPos(double x, double y, double z)
{
	xPos = x;
	yPos = y;
	zPos = z;
}

void CubePrimitive::setRot(double x, double y, double z)
{
	xRot = x;
	yRot = y;
	zRot = z;
}

void CubePrimitive::setSize(double sideLength)
{
	size = sideLength;
}

void CubePrimitive::setColor(double r, double b, double g)
{
	this->r = r;
	this->g = g;
	this->b = b;
}

void CubePrimitive::loadDefaultTexture()
{
	GLbyte* bits;
	texture = new QImage("/textures/wallTex.jpg");
	heightMap = new QImage("/textures/wallNormal.jpg");
	cout << texture->format() << endl;
	bits = (GLbyte*)texture->bits();


	//color map
	glGenTextures(1, &texID);
	glBindTexture(GL_TEXTURE_2D, texID);

	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	//glTexImage2DMultisample()
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture->width(), texture->height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, bits);

	//free(bits);

	//height map
	bits = (GLbyte*)heightMap->bits();
	glGenTextures(1, &heightID);
	glBindTexture(GL_TEXTURE_2D, heightID);

	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, heightMap->width(), heightMap->height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, bits);
	//free(bits);

	glActiveTexture(GL_TEXTURE1);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, heightID);
	glActiveTexture(GL_TEXTURE0);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texID);
}

GLuint CubePrimitive::getTexID()
{
	return texID;
}
